**************
*** TABLES ***
**************
	
	*** Load data
		use "$output/combined_data.dta", clear
	
	*** Define income groups
		local data pwt
		summ `data' if ccode == "USA" & year == 1960 
		local usa = `r(mean)'
		summ pwt if year == 1960, det 
		local lowthresh = `r(p25)'/`usa'
		local highthresh = `r(p75)'/`usa'
		gen us = `data' if ccode == "USA"
		sort year us
		by year: replace us = us[_n-1] if missing(us) & !missing(us[_n-1])
		gen catl = `lowthresh'*us
		gen catu = `highthresh'*us
		gen cat = "H" if `data' >= catu & !missing(`data')
		replace cat = "M" if `data' < catu & `data' >=catl & !missing(`data')
		replace cat = "L" if `data' < catl 
		keep `data' cat ccode year
		drop if year < 1960
		drop if missing(`data')
		gen fixed_ = year == 1960
		bys ccode: egen fixed = max(fixed_)
		drop fixed_
		reshape wide `data' cat, i(ccode fixed) j(year)
		gen gr1960 = (log(`data'1969/`data'1960)/(9))*100
		gen gr1970 = (log(`data'1979/`data'1970)/(9))*100
		gen gr1980 = (log(`data'1989/`data'1980)/(9))*100
		gen gr1990 = (log(`data'1999/`data'1990)/(9))*100
		gen gr2000 = (log(`data'2009/`data'2000)/(9))*100
		gen gr2010 = (log(`data'2019/`data'2010)/(9))*100
		keep ccode gr* fixed cat19*0 cat20*0
		
		forval decade = 1960(10)2010 {
			gen neg`decade' = gr`decade'<0 & !missing(gr`decade')
		}
		foreach cat in H M L {
			file open avg using "$tables/avggrowth_`cat'.tex", write replace
			file open neg using "$tables/neggrowth_`cat'.tex", write replace
			file open count using "$tables/count_`cat'.tex", write replace
			forval decade = 1960(10)2010 {
				summ gr`decade' if cat`decade' == "`cat'"
				local mean: di %9.2fc `r(mean)'
				file write avg "& `mean' "
				summ neg`decade' if cat`decade' == "`cat'"
				local mean: di %9.2fc `r(mean)'
				file write neg "& `mean' "
				count if !missing(gr`decade') & cat`decade' == "`cat'"
				local mean: di %9.0fc `r(N)'
				file write count "& `mean' "
			}
			file close avg
			file close neg
			file close count 
		}
		